<?php

// use Illuminate\Support\Facades\Schema;
use Jialeo\LaravelSchemaExtend\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->comment = '用户表';
            
            $table->increments('id');
            $table->uuid('uuid')->index();
            
            $table->string('name', 100)->index()->comment('昵称');
            $table->string('real_name', 50)->nullable()->comment('真实姓名');

            $table->string('number', 50)->index()->nullable()->comment('编号');
            $table->string('phone', 20)->index()->nullable()->comment('手机号');
            $table->char('gender', 2)->nullable()->comment('性别');
            $table->string('headimg')->nullable()->comment('头像');
            $table->string('email')->nullable()->comment('email');
            $table->date('birthday')->nullable()->comment('生日');
            $table->string('password')->nullable()->comment('md5后的密码');

            $table->string('openid', 64)->index()->nullable()->comment('openid');

            // 地区信息
            $table->string('provice', 100)->nullable()->comment('省份');
            $table->string('city', 100)->nullable()->comment('城市');
            $table->string('area', 100)->nullable()->comment('地区');


            $table->tinyInteger('level')->default(1)->comment('会员等级， 默认为1为普通会员'); //会员等级

            // 会员卡类型及到期时间
            $table->tinyInteger('vip_type')->default(1)->index()->comment('VIP类型, 一般付费会员');
            $table->string('vip_number', 50)->index()->nullable()->comment('VIP编号');
            $table->boolean('vip_status')->default(0)->index()->comment('VIP状态 0为未开通 1为已开通  2为过期');
            $table->timestamp('vip_start_at')->nullable()->comment('VIP开通时间');
            $table->timestamp('vip_end_at')->nullable()->comment('VIP到期时间');

            $table->boolean('is_inviter')->default(1)->comment('是否拥有邀请其它用户的权限');
            $table->string('invite_poster', 255)->nullable()->comment('邀请海报');

            // 分销系统
            $table->integer('inviter_id')->nullable()->comment('邀请人id');  // 父id
            $table->integer('inviter_index')->default(0)->comment('邀请层级，0为第1层');    // 层级
            $table->timestamp('invited_at')->nullable()->comment('邀请人时间'); // 邀请于
            $table->integer('invite_score')->nullable()->comment('邀请人获得的积分');    // 层级
            $table->integer('invite_money')->nullable()->comment('邀请人获得的红包');    // 层级

            $table->string('signature', 1000)->nullable()->comment('签名');
            $table->string('description', 10000)->nullable()->comment('描述');
            $table->string('tags', 255)->nullable()->comment('标签');

            $table->timestamp('last_active_at')->nullable()->comment('最后活跃时间');
            $table->timestamp('blocked_at')->nullable()->comment('拉黑时间'); // 拉黑于

            $table->tinyInteger('status')->index()->default(1)->comment('1为正常 0为blocked');
            $table->tinyInteger('type')->index()->default(1)->comment('1为普通用户   2为管理员用户  3为虚拟用户（管理员后台创建）');  // 1为普通用户   2为管理员用户  3为虚拟用户

            $table->string('wechat', 255)->nullable()->comment('微信名');        // 备注
            $table->string('remark', 255)->nullable()->comment('备注');        // 备注

            $table->text('options')->nullable()->comment('');

            $table->string('client_type', 20)->index()->nullable()->comment('注册来源类型');
            $table->string('client_sub_type', 20)->index()->nullable()->comment('注册来源子类型');

            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('social_accounts', function (Blueprint $table) {
            $table->comment = '用户社交帐号表';
            
            $table->increments('id');
            $table->uuid('uuid')->index();

            $table->integer('user_id')->unsigned()->index()->comment('用户id');

            $table->string('openid', 35)->index()->comment('openid');
            $table->string('unionid', 35)->index()->nullable()->comment('unionid');

            $table->string('headimgurl', 255)->nullable()->comment('头像');
            $table->string('nickname', 50)->nullable()->comment('昵称');

            $table->string('client_name', 25)->index()->nullable()->comment('应用名');     // 名称
            $table->string('openid_type', 25)->index()->nullable()->comment('miniapp小程序  公众号official_acount   手机应用app');     // 类型：miniapp小程序  公众号official_acount   手机应用app

            $table->string('type', 10)->index()->comment('wechat qq  facebook  weibo');    // wechat qq  facebook  weibo

            $table->softDeletes();
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('users');
        Schema::dropIfExists('social_accounts');
    }
}
